package com.netease.nim.uikit.business.session.viewholder;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.method.LinkMovementMethod;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.session.emoji.MoonUtil;
import com.netease.nim.uikit.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.netease.nim.uikit.common.util.sys.ScreenUtil;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nimlib.sdk.msg.model.IMMessage;

import java.util.Map;

/**
 * Created by zhoujianghua on 2015/8/4.
 */
public class MsgViewHolderText extends MsgViewHolderBase {

    protected TextView bodyTextView;
    protected TextView titleTextView;
    private ImageView img;
    private View ex;
    View parent;
    private ZuiWenData zuiwendata;

    public MsgViewHolderText(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.nim_message_item_text;
    }

    @Override
    protected void inflateContentView() {
        zuiwendata = getZuiwenItemInfo(message);
        parent = findViewById(R.id.parent);
        bodyTextView = findViewById(R.id.nim_message_item_text_body);
        titleTextView = findViewById(R.id.nim_message_item_text_title);
        ex = findViewById(R.id.ex);
        img = findViewById(R.id.zuiwenimg);
    }

    @SuppressLint("CheckResult")
    @Override
    protected void bindContentView() {
        if (zuiwendata != null) {
            ex.setVisibility(View.VISIBLE);
            bodyTextView.setText("追问问题，问题详情请点击查看");
            titleTextView.setText(zuiwendata.text);
            RequestOptions options = new RequestOptions();
            options.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .format(DecodeFormat.PREFER_RGB_565)
                    .centerInside()
                    .error(R.drawable.nim_image_default)
                    .placeholder(R.drawable.nim_image_default);
            Glide.with(context)
                    .applyDefaultRequestOptions(options)
                    .load(zuiwendata.imageUri)
                    .into(img);
            layoutDirection(parent);
        } else {
            ex.setVisibility(View.GONE);
            MoonUtil.identifyFaceExpression(NimUIKit.getContext(), bodyTextView, getDisplayText(), ImageSpan.ALIGN_BOTTOM);
            bodyTextView.setMovementMethod(LinkMovementMethod.getInstance());
            bodyTextView.setOnLongClickListener(longClickListener);
            layoutDirection(bodyTextView);
        }
        bodyTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick();
            }
        });
    }

    protected void onItemClick() {
        if (zuiwendata != null) {
//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            intent.setAction("android.intent.action.VIEW");
//            Uri content_url = Uri.parse(zuiwendata.uri);
//            intent.setData(content_url);
            // context.startActivity(intent);
            showZuiwenInfo(zuiwendata.uri);
        }
    }

    private void layoutDirection(View view) {
        if (isReceivedMessage()) {
            view.setBackgroundResource(NimUIKitImpl.getOptions().messageLeftBackground);
            if (view instanceof TextView) {
                ((TextView) view).setTextColor(Color.BLACK);
            }
            view.setPadding(ScreenUtil.dip2px(15), ScreenUtil.dip2px(8), ScreenUtil.dip2px(10), ScreenUtil.dip2px(8));
        } else {
            view.setBackgroundResource(NimUIKitImpl.getOptions().messageRightBackground);
            if (view instanceof TextView) {
                ((TextView) view).setTextColor(Color.WHITE);
            }
            view.setPadding(ScreenUtil.dip2px(10), ScreenUtil.dip2px(8), ScreenUtil.dip2px(15), ScreenUtil.dip2px(8));
        }
    }

    @Override
    protected int leftBackground() {
        return 0;
    }

    @Override
    protected int rightBackground() {
        return 0;
    }

    protected String getDisplayText() {
        return message.getContent();
    }


    private void showZuiwenInfo(String url) {
        ZuiWenDialog zd = new ZuiWenDialog(context, R.style.dialog, "");
        zd.setUri(url);
        zd.show();
                /*AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.zuiwen_dialog, null);
        WebView webView = (WebView) view.findViewById(R.id.content);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
        builder.setView(view);
        builder.setPositiveButton(R.string.cancel, null);
        builder.show();*/

    }

    public static ZuiWenData getZuiwenItemInfo(IMMessage msg) {
        Map<String, Object> ext = msg.getRemoteExtension();
        if (ext != null) {
            if (ext.get("zuiwenuri") == null) {
                return null;
            } else {
                ZuiWenData zd = new ZuiWenData();
                zd.imageUri = IpUtils.getOrganizationUrl() + ext.get("zuiwenimguri");
                zd.uri = IpUtils.getOrganizationUrl() + ext.get("zuiwenuri");
                zd.text = (String) ext.get("zuiwentxt");
                return zd;
            }
        } else {
            return null;
        }

    }
}
